package beans;


import engine.BIEngine0_1.AlgorithmThread;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * 目的是为了记录同一个批量任务中已经上传过的算法文件，提高效率，不重复上传
 */
public enum UploadedAlgorithmFile {
	UPLOADED_ALGORITHM_FILE;
	/**
	 * key:batchTaskHash+algorithmId
	 * value:ftp algorithm file uri
	 */
	private HashMap<String, List<String>> uploadedAlgorithmIds;

	UploadedAlgorithmFile() {
		this.uploadedAlgorithmIds = new HashMap<>();
	}


	/**
	 * 返回已经存过的算法ftp文件list
	 * @param algorithmId
	 * @return
	 */
	public List<String> getFtpAlgorithmUri(String algorithmId)
	{
		synchronized (AlgorithmThread.class)
		{
			if(this.uploadedAlgorithmIds.containsKey(algorithmId))
			{
				return this.uploadedAlgorithmIds.get(algorithmId);
			}else{
				return null;
			}
		}
	}

	/**
	 * 将上传过的算法ftp文件list存进来
	 * @param list
	 * @param algorithmId
	 */
	public void recordUploadAlgorithmFile(List<String> list, String algorithmId)
	{
		synchronized (AlgorithmThread.class)
		{
			if(this.uploadedAlgorithmIds==null)
				this.uploadedAlgorithmIds = new HashMap<>();
			this.uploadedAlgorithmIds.put(algorithmId,list);
		}

	}
	/**
	 * 用于记录同一个批量任务里面，有哪些算法文件已经上传过了
	 * @param algorithmId
	 * @return
	 */
	public boolean isAlgorithmFileUploaded(String algorithmId) {
		if(this.uploadedAlgorithmIds==null)
			this.uploadedAlgorithmIds = new HashMap<>();
		return this.uploadedAlgorithmIds.containsKey(algorithmId);
	}

	/**
	 * 当一个批量任务完成后，需要将其对应的东西清空
	 * @param taskHash batchTaskHash
	 */
	public void cleanAlgorithmFileUri(String taskHash) {
		if(this.uploadedAlgorithmIds!=null)
		{
			Iterator<Map.Entry<String,List<String>>> one = this.uploadedAlgorithmIds.entrySet().iterator();
			while(one.hasNext())
			{
				if(one.next().getKey().contains(taskHash))
					one.remove();
			}
		}
	}
}
